
Page({

  /**
   * 页面的初始数据
   */
  data: {
    schema:[],
    formData:{
      visualEffect: 'none',
      value: 0,
      keepScreenOn: true
    },
    title: '',
    result: '',
    remark: '',
    webUrl: '',
    apiName: ''
  },
  onLoad(options) {
    console.log('options', options)
    wx.setNavigationBarTitle({
      title: options.title,
    })
    
    this.setData({
      title: options.title,
      zh:options.zh,
      remark: options.remark,
      webUrl: options.webUrl
    })
    if(options.zh === 'wx.setVisualEffectOnCapture') {
      this.setData({
        schema: [
          {
            label:'截屏/录屏时的表现，仅支持 none / hidden，传入 hidden 则表示在截屏/录屏时隐藏屏幕',
            prop:'visualEffect',
            type:'picker',
            labelOptions:['none','hidden'],
            valueOptions:['none', 'hidden'],
          }
        ]
      })
    }
    if(options.zh === 'wx.setScreenBrightness') {
      this.setData({
        schema: [
          {
            label:'屏幕亮度值，范围 0 ~ 1，0 最暗，1 最亮。在安卓端支持传入特殊值 -1，表示屏幕亮度跟随系统变化',
            prop:'value',
            type:'number'
          }
        ]
      })
    }
    if(options.zh === 'wx.setKeepScreenOn') {
      this.setData({
        schema: [
          {
            label:'是否保持屏幕常亮',
            prop:'keepScreenOn',
            type:'switch',
          }
        ]
      })
    }
  },
  getFormData(e) {
    console.log('formdata==', e.detail.formData);
    this.setData({
      formData: e.detail.formData
    })
  },
  setVisualEffectOnCapture(e) {
    const that = this;
    wx.setVisualEffectOnCapture({
      visualEffect: this.data.formData.visualEffect,
      success(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      },
      fail(err) {
        that.setData({
          result: err,
          apiName: e.currentTarget.dataset.apiname
        })
      }
    })
  },
  setScreenBrightness(e) {
    const that = this;
    wx.setScreenBrightness({
      value: this.data.formData.value,
      success(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      },
      fail(err) {
        that.setData({
          result: err,
          apiName: e.currentTarget.dataset.apiname
        })
      }
    })
  },
  setKeepScreenOn(e) {
    const that = this;
    wx.setKeepScreenOn({
      keepScreenOn: this.data.formData.keepScreenOn,
      success(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      },
      fail(err) {
        that.setData({
          result: err,
          apiName: e.currentTarget.dataset.apiname
        })
      }
    })
  },
  onUserCaptureScreen(e) {
    wx.onUserCaptureScreen((res) => {
      console.log('用户截屏了')
        this.setData({
          result: '用户截屏了: 事件戳' + new Date().getTime(),
          apiName: this.data.zh === 'wx.onUserCaptureScreen' ? 'wx.onUserCaptureScreen' : ''
        })
        return {
            query: "parameter=test", // 通过截屏图片打开小程序的query参数
            promise: new Promise((resolve) => { // 通过promise延时传递小程序的query参数
                setTimeout(() => {
                    resolve({
                        query: "parameter=test2",
                    })
                }, 1000) // 在1秒内对query进行解析
            })
        }
    })
  },
  offUserCaptureScreen() {
    wx.offUserCaptureScreen()
  },
  getScreenBrightness(e) {
    const that = this;
    wx.getScreenBrightness({
      success(res) {
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      },
      fail(err) {
        that.setData({
          result: err,
          apiName: e.currentTarget.dataset.apiname
        })
      }
    })
  },
  onUnload() {
  }
})